//
//  ShareDataHandle.m
//  iNews
//
//  Created by lanou3g on 14-12-8.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "ShareDataHandle.h"
#import "HomeModel.h"
#import "DetailModel.h"

#import "NetRequest.h"

//首页url公共部分
#define kBeginPart @"http://c.3g.163.com/nc/article/headline/"

//头条轮播图另三张图的url
#define kBigThreeUrl @"http://c.3g.163.com/nc/ad/headline/android/0-3.html"

//详情web页面数据url公共部分
#define kWebViewUrl @"http://c.3g.163.com/nc/article/"

//图片页面
#define kPicViewUrl @"http://c.3g.163.com/photo/api/set/"

//天气接口
#define kWeatherUrl @"http://c.3g.163.com/nc/weather/5YyX5LqsfOWMl%2BS6rA%3D%3D.html"

static ShareDataHandle *share = nil;

@implementation ShareDataHandle

- (void)dealloc
{
    [_dataArray release];
    [_numberArr release];
    [_threeArray release];
    [_detailArray release];
    [_picArray release];
    
    [super dealloc];
}

+ (ShareDataHandle *)shareDataHandle
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        //在哪释放????
        share = [[ShareDataHandle alloc] init] ;
        
    });
    
    return share;
}

#pragma mark 解析首页页面数据
- (void)parseDataWithCenterKeyString:(NSString *)centerStr numberStr:(NSString *)numStr
{
    
    //拼接得到url
    NSString *str = [NSString stringWithFormat:@"%@%@/%@-20.html",kBeginPart,centerStr,numStr];
    
    __block ShareDataHandle *weakSelf = self;
    
    [NetRequest netReuestWithUrlString:str block:^(NSData *data) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *arr = dict[centerStr];
        
        NSMutableArray *tempArr;
      //  NSMutableArray *array;
        
        //判断是刷新还是加载数据
        if ([numStr isEqualToString:@"0"] ) {
            tempArr = [NSMutableArray array];
         //   array = [NSMutableArray array];
        }else{
            tempArr = [NSMutableArray arrayWithArray:weakSelf.dataArray];
         //  array = [NSMutableArray arrayWithArray:self.numberArr];
        }
        
        for (NSDictionary *d in arr) {
             NSArray *a = [d allKeys];
            
            //取小cell,三图cell的第一页内容
            if ((a.count == 14 || a.count == 12 || a.count == 15) || [d isEqual:arr[0]]) {
               
                HomeModel *model = [[HomeModel alloc] init];
                [model setValuesForKeysWithDictionary:d];
               //NSLog(@"%@",model.replyCount);
                [tempArr addObject:model];
                
                //保存以便读取每个cell处字典key值,便于判断cell种类
                [weakSelf.numberArr addObject:d];
                
                [model release];
              //  NSLog(@"%@",weakSelf.numberArr);
            }
        }
        weakSelf.dataArray = [NSMutableArray arrayWithArray:tempArr];
    }];
    
}

#pragma mark 解析大cell另三张图的信息
- (void)parseThreePicMessage
{
    __block ShareDataHandle *weakSelf = self;
    
    [NetRequest netReuestWithUrlString:kBigThreeUrl block:^(NSData *data) {
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *arr = dict[@"headline_ad"];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        for (NSDictionary *d in arr) {
            
            HomeModel *model = [[HomeModel alloc] init];
            [model setValuesForKeysWithDictionary:d];
            [tempArr addObject:model];
            [model release];
            
            }
        
        weakSelf.threeArray = [NSMutableArray arrayWithArray:tempArr];
        
        
    }];

}

#pragma mark 解析详情web页面的数据
- (void)parseDetailDataWithParam:(NSString *)str
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@/full.html",kWebViewUrl,str];
    
    __block ShareDataHandle *blockSelf = self;
    NSMutableArray *tempArr = [NSMutableArray array];
   [NetRequest netReuestWithUrlString:urlStr block:^(NSData *data) {
      
       NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
       
       DetailModel *dModel = [[DetailModel alloc] init];
       [dModel setValuesForKeysWithDictionary:dic[str]];
       
       [tempArr addObject:dModel];
       [dModel release];
       
       blockSelf.detailArray = [NSMutableArray arrayWithArray:tempArr];
       
   }];
}

#pragma mark 解析图片展示页面的数据
- (void)parsePicViewDataWithString:(NSString *)str
{
    //先剪切出两个数据
    NSString *string = [str substringFromIndex:[str length]- 10];
    
    NSArray *a = [string componentsSeparatedByString:@"|"];
    //最后的url字符串
    NSString *urlStr = [NSString stringWithFormat:@"%@%@/%@.json",kPicViewUrl,a[0],a[1]];
    
 //   NSLog(@"%@",urlStr);
    
    NSMutableArray *tempArray = [NSMutableArray array];
    __block ShareDataHandle *blockSelf = self;
    [NetRequest netReuestWithUrlString:urlStr block:^(NSData *data) {
        
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
       // NSLog(@"%@",dic);
        
        DetailModel *dModel = [[DetailModel alloc] init];
        [dModel setValuesForKeysWithDictionary:dic];
        
        [tempArray addObject:dModel];
        
        
        
        [dModel release];
        
        blockSelf.picArray = [NSMutableArray arrayWithArray:tempArray];
    }];
    
    
    
 //   NSLog(@"picArr:%@",self.picArray);
    
}

#pragma mark 解析天气的数据
- (void)parseWeatherData
{
    [NetRequest netReuestWithUrlString:kWeatherUrl block:^(NSData *data) {
       
        
        
    }];
}













@end
